/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.EntityConstants;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.Service;
import han.bifang.gwt.client.common.entity.Version;
import han.bifang.gwt.client.common.util.StringUtils;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.service.ServiceConstants;
import han.bifang.gwt.client.service.ServiceContentConstants;
import han.bifang.gwt.client.service.ServiceMessages;

import java.util.ArrayList;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class ModifyVersionBasicContent extends Content {

	private BifangGWTServiceAsync service = null;

	private GwtContext context = null;

	private ServiceMessages cons;

	private VerticalPanel panel = null;

	
	private CheckBox defaultVersion;
	private CheckBox publishNow;
	private TextArea desc;

	private String id;
	private ArrayList ds = null;

	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}

			ds = (ArrayList) res.getResponse();

			Version v = (Version) ds.get(0);
			Object b = ds.get(1);
			ArrayList qos = (ArrayList) ds.get(2);
			context.put(ServiceConstants.VERSION, v);
			context.put(ServiceConstants.BINDING, b);
			context.put(ServiceConstants.VERSION_QOS, qos);

			Grid table = new Grid(4, 2);
			table.setCellPadding(1);
			table.setCellSpacing(1);
			table.setBorderWidth(1);

			ColumnFormatter cf = table.getColumnFormatter();
			cf.setWidth(0, "15%");
			cf.setWidth(1, "45%");

			CellFormatter ff = table.getCellFormatter();
			ff.setStyleName(0, 0, "tablecol");
			ff.setStyleName(1, 0, "tablecol");
			ff.setStyleName(2, 0, "tablecol");
			ff.setStyleName(3, 0, "tablecol");

			table.setHTML(0, 0, cons.version());
			table.setHTML(1, 0, cons.defaultVersion());
			table.setHTML(2, 0, cons.published());
			table.setHTML(3, 0, cons.desc());

			

			defaultVersion = new CheckBox();
			publishNow = new CheckBox();

			desc = new TextArea();
			desc.setCharacterWidth(64);
			desc.setVisibleLines(6);
			if (context.get(ServiceConstants.VERSION) != null) {
				Version s = (Version) context.get(ServiceConstants.VERSION);
				
				defaultVersion.setChecked(s.isDefault());
				if(s.getStatus().equals(EntityConstants.VERSION_STATUS_PUBLISHED)){
					publishNow.setChecked(true);
				}
				desc.setText(s.getDescription());

			}

			table.setHTML(0, 1, v.getVersion());
			table.setWidget(1, 1, defaultVersion);
			table.setWidget(2, 1, publishNow);
			table.setWidget(3, 1, desc);

			Button bn = new Button(cons.next());
			bn.addClickListener(new ClickListener() {
				public void onClick(Widget send) {
					if (!validate())
						return;
					
					Version s = null;
					if(context.get(ServiceConstants.VERSION) != null)
						s = (Version)context.get(ServiceConstants.VERSION);
					else
						s = new Version();
					
					Service ser = (Service) context.get(ServiceConstants.SERVICE);
					s.setServiceId(ser.getId());
					s.setDescription(desc.getText());
					s.setServiceCode(ser.getServiceCode());
					
					if (publishNow.isChecked()) {
						s.setStatus(EntityConstants.VERSION_STATUS_PUBLISHED);
					} else {
						s.setStatus(EntityConstants.VERSION_STATUS_UNPUBLISHED);
					}
					s.setDefault(defaultVersion.isChecked());

					context.put(ServiceConstants.VERSION, s);

					listener.onContentChanged(
							ServiceContentConstants.MODIFY_VERSION_BASIC,
							ServiceContentConstants.MODIFY_VERSION_COMM, null);

				}
			});

			panel.add(table);
			panel.add(bn);
		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	public ModifyVersionBasicContent(ServiceMessages cons,
			BifangGWTServiceAsync service, GwtContext context, String id) {
		this.cons = cons;
		this.service = service;
		this.context = context;
		this.id = id;
		panel = new VerticalPanel();

		initWidget(panel);

	}

	public void onLoad() {
		panel.setSpacing(5);
		panel.setWidth("100%");

		HTML h = new HTML(cons.fillVersionBasicInfo());
		panel.add(h);
		
		RequestObject req = new RequestObject();
		req.setAction("han.bifang.gwt.server.action.service.GetVersionDetailAction");
		req.setRequest(id);
		service.call(req, cb);
		

	}
	

	private boolean validate() {
		
		if (!StringUtils.checkLenght(desc.getText(), 1, 1024)) {
			Window.alert(cons.lengthError(cons.desc(), "1", "1024"));
			return false;

		}

		return true;

	}

}
